/**
 * Created by haocongshun on 2016/7/27.
 */
$(function () {
    $("#projectTop").show();
    $("#projectTable").show();
    $("#setTop").hide();
    $("#setFrom").hide();
    getAll();
    bindset();
    bindprogress();
});
function bindprogress() {
    $(".process").click(function () {
        var id = $(this).parent().parent().children(".id").text();
        self.location='/TaskCollection/teacher/projectProgress.jsp?id='+id;
    });
}
//确认按钮和返回按钮绑定
function bind_enter_back(isSet) {
    $("#set-enter").click(function () {
        var dataSub = {};
        var url="/TaskCollection/Project/project.wst";
        dataSub.flag="isSet";
        dataSub.id=$("#name span").attr("data-projectid");
        dataSub.isSet=$("#setYN").val();
        if(dataSub.isSet=="否"&&isSet=="是"){
            alert("取消立项，则会解散小组，暂不可取消立项！");
            return;
        }
        dataSub.classify=$("#classify").val();
        $.post(url,dataSub,function (result) {
            if(result.msg){
                $("#projectTop").show();
                $("#projectTable").show();
                $("#setTop").hide();
                $("#setFrom").hide();
                $("#project tr:not(:first)").html("");
                getAll();
                bindset();
            }else{
                alert("请求失败！")
            }
        },"json");

    });
    $("#set-back").click(function () {
        $("#projectTop").show();
        $("#projectTable").show();
        $("#setTop").hide();
        $("#setFrom").hide();
        $("#project tr:not(:first)").html("");
        getAll();
        bindset();
        bindprogress();
    });
}
//绑定set（管理按钮）
function bindset() {
    $(".set").click(function () {
        var dataSet={};
        dataSet.flag="set";
        dataSet.projectID = $(this).parent().parent().children(".id").text();
        var url = "/TaskCollection/Project/project.wst";
        $.post(url,dataSet,function (result) {
            if(result.project==null){
                alert("请求失败，请重试！");
            }else {
                $("#projectTop").hide();
                $("#projectTable").hide();
                $("#setTop").show();
                $("#setFrom").show();
                projectSet(result)
            }
        },"json");
    });
}
//获取页面数据
function projectSet(result) {
    var id = result.project.project_id;
    var name = result.project.project_name;
    var studentName = result.project.student.student_name;
    var introduction = result.project.project_introduction;
    $("#name span").attr("data-projectid",id);
    $("#name span").text(name);
    $("#student span").text(studentName);
    $("#introduction textarea").val(introduction);
    var url = "/TaskCollection/projectClassify";
    var data={};
    var option="";
    $.post(url,data,function (result) {
        if(result==null){
            
        }else {
            for(var i= 0;i<result.getAll.length;i++){
                option+="<option value='"+result.getAll[i].projectClassify_id+"'>"+result.getAll[i].projectClassify_name+"</option>";
            }
            $("#classify").html(option);
        }
    },"json");
    bind_enter_back(result.project.project_isSet);
}
function getAll() {
    var dataAll = {};
    dataAll.flag = "getAll";
    var url = "/TaskCollection/Project/project.wst";
    $.ajaxSetup({
        async : false
    });
    $.post(url,dataAll,function (result) {
        for(var i = 0;i < result.getAll.length;i++){
            var projectClassifyName = result.getAll[i].projectClassify.projectClassify_name;
            projectClassifyName=projectClassifyName==null?"":projectClassifyName;
            var projectTeamName = result.getAll[i].projectTeam.projectTeam_name;
            projectTeamName=projectTeamName==null?"":projectTeamName;
            var newrow = "<tr>" +
                            "<td class='id'>"+result.getAll[i].project_id+"</td>" +
                            "<td>"+result.getAll[i].project_name+"</td>" +
                            "<td>"+result.getAll[i].project_isSet+"</td>" +
                            "<td>"+result.getAll[i].student.student_name+"</td>" +
                            "<td>"+projectTeamName+"</td>" +
                            "<td>"+projectClassifyName+"</td>" +
                            "<td><input class='btn set' type='button' value='管理'></td> " +
                            "<td><input class='btn process' type='button' value='进度'></td>" +
                          "</tr>";
            $("#project tr:last").after(newrow);
        }
    },"json");
}
